Ext.define('PJ.view.registrationmanagement.ApplicationAuditList', {
    extend: 'AM.default.DefaultList',
    alias: ['widget.applicationAuditList'],
    requires: ['AM.default.DefaultDateField', 
               'AM.default.DateTime', 
               'AM.default.DefaultSearchForm',
               'AM.default.jsonp.DefaultLoadJsonp'//jsonp加载数据
               ],
    
    initComponent: function(){
        var me = this;
        me.columns = [{
            header: '审核状态',
            dataIndex: 'c.auditing_State',
            width: 80,
            //sortable: false,
            renderer: function(v, meta, record){
            	meta.style="padding-bottom: 0px;padding-top: 0px;line-height: 19px;padding-left: 0px;";
                var text = "",tipText="";
                if (v == 3) {
                	tipText="报名待审核";
                	text= '<div style="border-left:5px solid white;height:19px;padding-left:2px;">报名待审核</div>';
                }
                else if (v == 1) {
                	text= '<div style="border-left:5px solid green;height:19px;padding-left:2px;">报名通过</div>';
                	tipText="报名通过";
                }else if (v == 2) {
                	tipText="点击查看驳回原因";
                	text= '<div style="border-left:5px solid red;height:19px;cursor: pointer;padding-left:2px;"><span style="cursor: pointer;color:blue;">报名未通过</span></div>';
                }else if(v == 4){
                	tipText="撤销待审核";
                	text= '<div style="border-left:5px solid white;height:19px;padding-left:2px;">撤销待审核</div>';
                }else if(v == 5){
                	tipText="报名通过（撤销未通过）";
                	text= '<div style="border-left:5px solid green;height:19px;padding-left:2px;">报名通过(<span style="cursor: pointer;color:blue;">撤销未通过</span>)</div>';
                }
                meta.tdAttr = 'data-qtip="' + tipText + '" auditStatus=' + record.get("c.auditing_State") + ' classUserId=' + record.get("classUserId") + ' userId=' + record.get("userId");
                return text;
            },
    		listeners: {
    			click: function(g, clm){
//    				var userId = clm.getAttribute("userId");
    				var auditStatus = clm.getAttribute("auditStatus");
    				var classUserId = clm.getAttribute("classUserId");
    				if(auditStatus == 5){
    					var win = Ext.create('PJ.view.registrationmanagement.CheckEdit', {
        					title: "查看驳回原因",
        					flag:4,
        					grid: me,
        					isDisable:false,
        					isReadOnly:true
        				}).show();
        				var actionName = "load";
        				if(gj_onlineLearning_constant.loadType == "jsonp"){
        					actionName = "loadJsonp";
        				}
        				win.down("form").getForm().doAction(actionName,{
        					url:me.store.findRejectReasonUrl,
        					params:{
        						classUserId: classUserId
        					},
        					success:function(f,action){
        					}
        				});
    				}else if(auditStatus == 2){
    					var win = Ext.create('PJ.view.trainingmanagement.CheckEdit', {
        					title: "查看驳回原因",
        					flag:4,
        					grid: me,
        					isDisable:false,
        					isReadOnly:true
        				}).show();
        				var actionName = "load";
        				if(gj_onlineLearning_constant.loadType == "jsonp"){
        					actionName = "loadJsonp";
        				}
        				win.down("form").getForm().doAction(actionName,{
        					url:me.store.findCommentUrl,
        					params:{
        						classUserId: classUserId
        					},
        					success:function(f,action){
        					}
        				});
    				}else{
    					return;
    				}
    			}
    		}
        },{
        	header: '学员帐号',
        	dataIndex: 'bu.login_id',
        	width: 80
        },{
        	header: '姓名',
        	dataIndex: 'bu.user_Name',
        	width: 80,
        	editor: {
        		xtype: 'textfield',
        		maxLength: 10,
        		listeners: {
        			blur: function(item){
        				var grid = item.up("grid");
        				var record = grid.getSelectionModel().getLastSelected();
        				var bforeLimit = record.get("i.userName");
        				var searchId = record.get("userId");
        				var value = item.getValue(); // 得到修改后的值
        				if(value.length > 10){
                			Orchid.alert('提示','该输入项最多输入10个字符！');
                			return;
                		}
        				if(value == null || value ==""){
        					Ext.Msg.alert('提示', "请重新输入要更改的人员姓名");
        				}else if(value==bforeLimit){
        					return;
        				}else{
        					if(gj_onlineLearning_constant.loadType == "jsonp"){
        						var store = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
        							url: grid.store.updateUrl,
        							extraParams: {
        								userid:searchId,
        								userName:value,
        								"springmvc.TOKEN": item.token
        							},
        							loadType:gj_onlineLearning_constant.loadType
        						});
        						store.load(function(rec, operation, success){
        							if (success) {
        								Ext.Msg.alert('消息', operation.response.msg, function(){
        									grid.store.load();
        								});
        							}else {
        								Ext.Msg.alert('消息', operation.error);
        							}
        						});
        					}else if(gj_onlineLearning_constant.loadType == "ajax"){
        						Ext.Ajax.request({
        							url: grid.store.updateUrl,
        							params: {
        								userid:searchId,
        								userName:value,
        								"springmvc.TOKEN": item.token
        							},
        							success: function(response){
        								var data = response.responseText;
        								var json = Ext.decode(data, true);
        								if (json.success) {
        									Ext.Msg.alert('消息', json.msg, function(){
            									grid.store.load();
        									});
        								}
        								else {
        									Ext.Msg.alert('消息', json.msg);
        								}
        							}
        						});
        					}else{
        						return
        					}
        				}
        			}
        		}
        	}
        }, {
            header: '性别',
            dataIndex: 'bb.order_num',
            width: 45
        },{
            header: '出生年月',
            dataIndex: 'c.birthday',
            width: 70,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: '民族',
            dataIndex: 'bt.order_num',
            width: 50,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        },{
            header: '手机号',
            dataIndex: 'c.mobile_Telephone',
            width: 100
        }, {
            header: '单位',
            dataIndex: 'd.depname',
            flex:.8,
            renderer: function(value, meta, record){
            	if(record.get('depParentName')=="组织机构树"||record.get('depParentName')==null||record.get('depParentName')==""){
            		meta.tdAttr = 'data-qtip="' +value+'"';
            	}else{
            		meta.tdAttr = 'data-qtip="' + record.get('depParentName')+"--"+value + '"';
            	}
                return value;
            }
        },{
            header: '职级',
            dataIndex: 'bbb.order_num',
            width: 135,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: '单位及职务',
            dataIndex: 'c.duty_remark',
            flex:1,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, /*{
            header: '联系电话',
            dataIndex: 'bu.mobile_Telephone',
            flex:1,
            minWidth: 120,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        },*/ {
            header: '备注',
            dataIndex: 'c.remark',
            flex:.5,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }];
        me.store = Ext.create('PJ.' + prototype + '.registrationmanagement.ApplicationAuditList', {
        	isLoad: false
//            listeners: {
//            	load:function(){
//            		me.quotaOfPeople();
//            	}
//            }
        });
        me.loadType = gj_onlineLearning_constant.loadType;
        me.callParent(arguments);
    },
    /**
     * 同意报名
     */
    onExaminationPassedClick: function(){
    	var me=this;
    	var store=me.getStore();
    	var records=me.getSelectionModel().getSelection();
    	for(var i=0;i<records.length;i++){
    		if(records[i].get("c.auditing_State")==1){
    			Ext.Msg.show({
    				title: '提示',
    				msg  : '不允许对审核已通过的数据再次进行审核!',
    				icon :Ext.Msg.WARNING,
    				buttons:Ext.Msg.OK
    			});
    			return;
    		}
    		if(records[i].get("c.auditing_State")==4){
    			Ext.Msg.show({
    				title: '提示',
    				msg  : '请先处理撤销审核!',
    				icon :Ext.Msg.WARNING,
    				buttons:Ext.Msg.OK
    			});
    			return;
    		}
    	};
    	Ext.Msg.confirm({
    		title:'提示',
    		msg:'您确定要同意这些学员报名吗？',
    		icon:Ext.Msg.WARNING,
    		buttons:Ext.Msg.OKCANCEL,
    		fn:function(id){
    			if(id=='ok'){
    				var courseIds= new Array();
    				var userName = new Array();
    				Ext.Array.each(records,function(name,index){
    					courseIds.push(name.get('classUserId'));
    					userName.push(name.get('bu.user_Name'));
    				});
    				if(gj_onlineLearning_constant.loadType == "jsonp"){
    					var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
    						loadType: gj_onlineLearning_constant.loadType,
    						url: store.examinationPassedUrl,
    						extraParams: {
    							auditingState: 1,
    							searchClassUserIds:courseIds,
    							auditngNames:userName
    						}
    					});
    					store1.load(function(rec, operation, success){
    						if(success){
    							Ext.Msg.alert('消息', '操作成功！', function(){
    								var store = me.getStore();
    								store.load();
    							});
    						}else{
    							Ext.Msg.alert('消息', operation.error, function(){
    								win.close();
    							});
    						}
    					});
    				}else{
    					Ext.Ajax.request({
    						url:store.examinationPassedUrl,
    						params: {
    							auditingState: 1,
    							searchClassUserIds:courseIds,
    							auditngNames:userName
        					},
    						success:function(response){
    							var data=response.responseText;
    							var json=Ext.decode(data,true);
    							if(json.success){
    								Orchid.alert('提示','操作成功!',function(){
    									store.load();
    								});
    							}else{
    								Orchid.alert('提示',json.msg,function(){
    									store.load();
    								});
    							}
    						}
    					});
    				}
    			}
    		}
    	});
    },
    /**
     * 审核驳回
     */
    onAuditDismissedClick: function(){
    	var me=this;
    	var store=me.getStore();
    	var classUserIds = new Array;
    	var userName = new Array();
    	var records=me.getSelectionModel().getSelection();
    	for(var i=0;i<records.length;i++){
    		if(records[i].get("c.auditing_State")==2){
    			Ext.Msg.show({
    				title: '提示',
    				msg  : '不能对报名未通过的数据再次进行驳回!',
    				icon :Ext.Msg.WARNING,
    				buttons:Ext.Msg.OK
    			});
    			return;
    		}
    		if(records[i].get("c.auditing_State")==4){
    			Ext.Msg.show({
    				title: '提示',
    				msg  : '请先处理撤销审核!',
    				icon :Ext.Msg.WARNING,
    				buttons:Ext.Msg.OK
    			});
    			return;
    		}
    	};
    	for(var i=0;i<records.length;i++){
    		classUserIds.push(records[i].get('classUserId'));
    		userName.push(records[i].get('bu.user_Name'));
    	}
    	Ext.create('PJ.view.trainingmanagement.CheckEdit',{
    		title: "驳回重报原因",
    		flag:1,
    		grid: me,
    		//commentUrl: store.commentUrl,
    		classUserIds:classUserIds,
    		userName:userName,
    		isReadOnly: false
    	});
    },
    /**
     * 同意撤销申请
     */
    onAgreedToWithdrawClick: function(){
    	var me=this;
    	var store=me.getStore();
    	var records=me.getSelectionModel().getSelection();
    	for(var i=0;i<records.length;i++){
    		if(records[i].get("c.auditing_State")!=4){
    			Ext.Msg.show({
    				title: '提示',
    				msg  : '只允许对状态为撤销待审核的人员执行此操作！',
    				icon :Ext.Msg.WARNING,
    				buttons:Ext.Msg.OK
    			});
    			return;
    		}
    	};
    	Ext.Msg.confirm({
    		title:'提示',
    		msg:'您确定要同意这些数据的撤销申请操作吗?',
    		icon:Ext.Msg.WARNING,
    		buttons:Ext.Msg.OKCANCEL,
    		fn:function(id){
    			if(id=='ok'){
    				var courseIds= new Array();
    				var searchUserIds = new Array();
    				var userName = new Array();
    				Ext.Array.each(records,function(name,index){
    					courseIds.push(name.get('classUserId'));
    					searchUserIds.push(name.get('userId'));
    					userName.push(name.get('bu.user_Name'));
    				});
    				if(gj_onlineLearning_constant.loadType == "jsonp"){
    					var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
    						loadType: gj_onlineLearning_constant.loadType,
    						url: store.agreedWithdrawUrl,
    						extraParams: {
    							searchClassUserIds:courseIds,
    							searchUserIds: searchUserIds,
    							openState: me.up('window').openState,
    							auditngNames:userName
    						}
    					});
    					store1.load(function(rec, operation, success){
    						if(success){
    							Ext.Msg.alert('消息', '操作成功！', function(){
    								var store = me.getStore();
    								store.load();
//    								me.quotaOfPeople();//人数统计
    							});
    						}else{
    							Ext.Msg.alert('消息', operation.error, function(){
    								win.close();
    							});
    						}
    					});
    				}else{
    					Ext.Ajax.request({
    						url:store.agreedWithdrawUrl,
    						params: {
    							searchClassUserIds:courseIds,
    							searchUserIds: searchUserIds,
    							openState: me.up('window').openState,
    							clickSource:1,
    							auditngNames:userName
        					},
    						success:function(response){
    							var data=response.responseText;
    							var json=Ext.decode(data,true);
    							if(json.success){
    								Orchid.alert('提示','操作成功!',function(){
    									store.load();
//    									me.quotaOfPeople();//人数统计
    								});
    							}else{
    								Orchid.alert('提示',json.msg,function(){
    									store.load();
    								});
    							}
    						}
    					});
    				}
    			}
    		}
    	});
    },
    /**
     * 驳回撤销申请
     */
    onRejectedRevocationClick: function(){
    	var me=this;
    	var store=me.getStore();
    	var classUserIds = new Array;
    	var userName = new Array;
    	var records=me.getSelectionModel().getSelection();
    	for(var i=0;i<records.length;i++){
    		if(records[i].get("c.auditing_State")!=4){
    			Ext.Msg.show({
    				title: '提示',
    				msg  : '只允许对状态为撤销待审核的人员执行此操作！',
    				icon :Ext.Msg.WARNING,
    				buttons:Ext.Msg.OK
    			});
    			return;
    		}
    	};
    	for(var i=0;i<records.length;i++){
    		classUserIds.push(records[i].get('userId'));
    		userName.push(records[i].get('bu.user_Name'));
    	}
    	Ext.create('PJ.view.registrationmanagement.CheckEdit',{
    		title: "驳回撤销申请原因",
    		flag:1,
    		grid: me,
    		commentUrl: store.rejectedRevocationUrl,
    		classUserIds:classUserIds,
    		userName:userName,
    		isReadOnly: false
    	});
    },
    //名额统计
    quotaOfPeople: function(){
    	var me = this;
        var quotaOfPeopleLabel = this.down("#quotaOfPeople");
        var classNameStr = me.classType;
        if (quotaOfPeopleLabel) {
            if(gj_onlineLearning_constant.loadType == "jsonp"){
            	var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
            		loadType: gj_onlineLearning_constant.loadType,
            		url: this.store.quotaOfPeopleUrl + '?searchClassID=' + this.up('window').classId
            	});
            	store1.load(function(rec, operation, success){
            		if(success){
            			var jsonData = operation.response.result;
            			quotaOfPeopleLabel.setText(
            					'本班名额<span style="color:red">' + jsonData.asignCount +
            					'</span>人，已报<span style="color:red">' + jsonData.reportedNum +
            					'</span>人，剩余<span style="color:red">' + jsonData.surplusCount +
            					'</span>人。', false);
            		}
            	});
            }else if(gj_onlineLearning_constant.loadType == "ajax"){
            	Ext.Ajax.request({
            		url: this.store.quotaOfPeopleUrl + '?searchClassId=' + this.up('window').classId,
            		success: function(response){
            			var data = response.responseText;
            			var json = Ext.decode(data, true);
            			if (json.success) {
            				var jsonData = json.result;
            				quotaOfPeopleLabel.setText(
            						'本班名额<span style="color:red">' + jsonData.asignCount +
                					'</span>人，已报<span style="color:red">' + jsonData.reportedNum +
                					'</span>人，剩余<span style="color:red">' + jsonData.surplusCount + 
                					'</span>人。', false);
            			}
            		}
            	});
            }
        }
    },
    //导出
    onExportClick: function(){
    	var me = this;
        var store = me.getStore();
        var params=store.proxy.extraParams;
        var href=store.exportUrl + "?";
        for(var param in params){
        	if(params[param] === undefined){
        		href+="&"+param+"=";
        	}else{
        		href+="&"+param+"="+params[param];
        	}
        }
    	href+="&searchSignUpState=1&searchSignUpState=5";
        href = encodeURI(href);
        var atag = Ext.get("exportResume");
        atag.set({
            "href": href
        });
        atag.dom.click();
    },
    listeners: {
        //点击列表数据时显示按钮事件
    	selectionchange: function(selModel, selected){
    		this.down('#' + this.oldId + "ExaminationPassed").setDisabled(selected.length === 0);//审核通过
    		this.down('#' + this.oldId + "AuditDismissed").setDisabled(selected.length === 0);//审核驳回
    		this.down('#' + this.oldId + "AgreedToWithdraw").setDisabled(selected.length === 0);//同意撤销申请
    		this.down('#' + this.oldId + "RejectedRevocation").setDisabled(selected.length === 0);//驳回撤销申请
    	},
        viewready: function(){
        	var me = this;
            this.addDocked([{
                dock: 'top',
                xtype: "searchform",
                columnCount: 3,
                fieldDefaults: {
                    xtype: "combo",
                    emptyText: "请选择",
                    editable: false,
                    queryMode: 'local',//'remote',//数据类型
                    valueField: 'value',
                    displayField: 'text',
                    
                    labelAlign: 'right',
                    labelWidth: 100,
                    maxLength: 100
                },
                items: [{
                    xtype: "textfield",
                    fieldLabel: '学员账号',
                    name: "searchLoginID",
                    emptyText: "请输入学员帐号",
                    columnWidth:.25
                },{
                    xtype: "textfield",
                    fieldLabel: '姓名',
                    name: "searchUserName",
                    emptyText: "请输入姓名",
                    columnWidth:.25
                }, {
                    xtype: "textfield",
                    fieldLabel: "单位",
                    name: "searchDepName",
                    emptyText: "请输入单位",
                    columnWidth:.25
                }, {
                    fieldLabel: "审核状态",
                    name: "searchSignUpState",
                    labelWidth: 80,
                    store: Ext.create("Ext.data.ArrayStore", {
    					fields: ['value', 'text'],
    					data: [[3, '报名待审核'], [1, '报名通过'], [2, '报名未通过'],[4,'撤销待审核'],[5,'报名通过（撤销未通过）']]
    				}),
                    columnWidth:.25
                }],
    			onResetClick: function(button){
                    var form = button.up('form');
                    var gridPanel = form.grid;
                    var newParams = {
                    		searchClassID:me.up('window').classId,
                			clickSource:me.up('window').clickSource,
                    };
                    gridPanel.getStore().proxy.extraParams = {};
                    var params = gridPanel.getStore().proxy.extraParams;
                    Ext.apply(params, newParams);
                    form.getForm().reset();
                },
    			onSelectClick : function(btn) {
    				var form = this;
    				if (form.grid) {
    					if (!form.getForm().isValid()) {
    						return;
    					}
    					var newParams = form.getValues();
    					var store = form.grid.getStore();
    					// 重置翻页属性
    					form.grid.resetPage();
    					// 4.0强制更改store参数写法
    					var params = store.proxy.extraParams;
    					Ext.apply(params, newParams);
    					store.load({
    						scope: form,
    						callback: function(records, operation, success){
    							if(success){
    								this.onAfterSearch(form);
//    								me.quotaOfPeople();//人数统计
    							}
    						}
    					});
    				} else {
    					Ext.Msg.show({
    								title : orchid_lang.promptWindowInfo.warningTitle,
    								msg : default_lang.defaultList.selectWarningMsg,
    								icon : Ext.Msg.WARNING,
    								buttons : Ext.Msg.OK
    							});
    				}
    			},
                grid: this
            },{
                xtype: 'toolbar',
                dock: 'top',
                displayInfo: true,
                items: [{
                    text: '同意报名',
                    iconCls: 'examinationPassedIco',
                    itemId: this.oldId + "ExaminationPassed",
                    disabled: true,
                    scope: this,
                    handler: this.onExaminationPassedClick
                }, '-', {
                    text: '驳回重报',
                    iconCls: 'auditDismissedIco',
                    itemId: this.oldId + "AuditDismissed",
                    disabled: true,
                    scope: this,
                    handler: this.onAuditDismissedClick
                }, '-',{
                    text: '同意撤销申请',
                    iconCls: 'examinationPassedIco',
                    itemId: this.oldId + "AgreedToWithdraw",
                    disabled: true,
                    scope: this,
                    handler: this.onAgreedToWithdrawClick
                }, '-', {
                    text: '驳回撤销申请',
                    iconCls: 'auditDismissedIco',
                    itemId: this.oldId + "RejectedRevocation",
                    disabled: true,
                    scope: this,
                    handler: this.onRejectedRevocationClick
                }, '-', {
                    text: '导出',
                    iconCls: 'deriveIco',
                    itemId: this.oldId + "Export",
                    disabled: false,
                    scope: this,
                    handler: this.onExportClick
                }/*,"-", {
                    xtype: 'label',
                    itemId: 'quotaOfPeople',
                    html: ''
                }*/, "->", "->", this.countSelectPanel]
            }]);
            // 触发权限过滤事件
            this.fireEvent("permission", this);
            var store = this.store;
            var params = store.proxy.extraParams;
            var newParams = {
            		searchClassID:me.up('window').classId,
        			clickSource:me.up('window').clickSource,
            };
            Ext.apply(params, newParams);
            store.isLoad = true;
            store.load(function(rec, operation, success){
//            	me.quotaOfPeople();//人数统计
            });
        },
        afterrender: function(){
            this.loadMask = new Ext.LoadMask(this, {
                msg: "正在加载中,请稍后...."
            });
        }
    }
});
